package yunquan.wang.modules.base.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import yunquan.wang.base.dao.springdata.BaseRepository;
import yunquan.wang.base.service.impl.BaseServiceImpl;
import yunquan.wang.modules.base.dao.RolesAuthorityDao;
import yunquan.wang.modules.base.dao.RolesinfoDao;
import yunquan.wang.modules.base.entity.Authority;
import yunquan.wang.modules.base.entity.RolesAuthority;
import yunquan.wang.modules.base.entity.Rolesinfo;
import yunquan.wang.modules.base.service.RolesinfoService;
/**
 *  Rolesinfo Service层 实现
 *  @author 王云权  wangyunquan@gmail.com
 *  用生成器 生成于 2011-09-30 22:37
 *
 */
@Service("rolesinfoService")
@Transactional(value="jpaTransaction")
public class RolesinfoServiceImpl extends BaseServiceImpl<Rolesinfo>
		implements RolesinfoService {
	private RolesinfoDao rolesinfoDao;
	private RolesAuthorityDao rolesAuthorityDao;
	@Resource(name="rolesinfoDao")
	public void setDao(RolesinfoDao rolesinfoDao) {
		this.rolesinfoDao = rolesinfoDao;
	}
 
	@Resource(name="rolesAuthorityDao")
	public void setRolesAuthorityDao(
			RolesAuthorityDao rolesAuthorityDao) {
		this.rolesAuthorityDao = rolesAuthorityDao;
	}


	public BaseRepository<Rolesinfo, String> getDao() {
		return rolesinfoDao;
	}


	@Override
	public Rolesinfo save(Rolesinfo entity, String authIds) {
		
		String authIdArray[] =authIds.split("\\,");
		entity=	rolesinfoDao.save(entity);
		List<RolesAuthority>	exsitsAuth=rolesAuthorityDao.findByRolesinfoId(entity.getId());
		Set<RolesAuthority>  rolesAuthority=new HashSet<RolesAuthority>();
		for(String authId:authIdArray)
		{
			Boolean exsits = false;
			for (RolesAuthority roles : exsitsAuth) {
				if (roles.getAuthority().getId().equals(authId)) {
					exsits = true;
				}
			}
			if(!exsits){
			RolesAuthority roau=new RolesAuthority();
			Authority authority=new Authority();
			authority.setId(authId);
			roau.setAuthority(authority);
			roau.setRolesinfo(entity);
			rolesAuthority.add(roau);
			}
		}
		rolesAuthorityDao.save(rolesAuthority);
		entity.setRolesAuthority(rolesAuthority);
 
		return entity;
	}

	@Override
	public void updateRulesinfo(Rolesinfo entity) {
	 
		rolesinfoDao.save(entity);
	}

}
